package com.spukmk3me.impl.libgdx;

import java.io.IOException;

/* $if SPUKMK3ME_DEBUG$ */
import com.spukmk3me.debug.Logger;
/* $endif$ */
import com.spukmk3me.io.FileHandle;
import com.spukmk3me.video.Image;
import com.spukmk3me.video.ImageCreationDetails;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;

public class LibgdxImage extends Image
{
    /**
     *  Constructor used by LibgdxResourceCreator
     */
    LibgdxImage() {}
    
    /**
     *  Constructor used by user to create a custom Image object with existing libgdx Texture.
     *  \details WARNING: do not add LibgdxImage created by this constructor to ResourceManager.
     *  @param texture
     */
    public LibgdxImage( Texture texture )
    {
        this.gdxTexture = texture;
    }

    @Override
    public byte getOpaqueStatus()
    {
        return TRANSPARENT;
    }

    @Override
    public int height()
    {
        if ( gdxTexture == null )
            return 0;
        
        return gdxTexture.getHeight();
    }

    @Override
    public int width()
    {
        if ( gdxTexture == null )
            return 0;
        
        return gdxTexture.getWidth();
    }

    @Override
    protected int _load() throws IOException
    {
        FileHandle file = getFileHandle();
        
        /* $if SPUKMK3ME_DEBUG$ */
        Logger.log( "Loading " + file.getPath() + "..." );
        /* $endif$ */
        
        if ( file.getLocation() == FileHandle.LOC_INTERNAL )
            gdxTexture = new Texture( Gdx.files.internal( file.getPath() ) );
        else if ( file.getLocation() == FileHandle.LOC_EXTERNAL )
            gdxTexture = new Texture( Gdx.files.external( file.getPath() ) );
        else
            return -2;
        
        if ( getCreationDetails() == null ) // Default setting for image without specific settings
            minFilter = magFilter = GL10.GL_NEAREST;
        else
        {
            ImageCreationDetails details = (ImageCreationDetails)getCreationDetails();
            
            minFilter = details.glMinFilter;
            magFilter = details.glMagFilter;
        }
        
        /* $if SPUKMK3ME_DEBUG$ */
        Logger.log( "done\n" );
        /* $endif$ */

        return 0;
    }

    @Override
    protected void _release()
    {
        gdxTexture.dispose();
        gdxTexture = null;
    }
    
    Texture gdxTexture;
    int     minFilter, magFilter;
}
